import json

import opensearchpy as OpenSearch

# from opensearchpy import OpenSearch

path_to_queries = 'queries.jsonl'
target_index_name = 'bm25'
field_name = 'body'

client = OpenSearch(
    hosts=[{'host': '3.112.66.241', 'port': 9200}],
    http_compress=True,  # enables gzip compression for request bodies
    http_auth=('admin', 'admin'),
    use_ssl=False,
    verify_certs=False,
    ssl_assert_hostname=False,
    ssl_show_warn=False
)


def deal_line(**kwargs):
    content = kwargs['line']
    key_names = kwargs['key_names']
    if len(content) == 0:
        return ""
    content = json.loads(content)[key_names[0]]

    print("content is:" + content)
    return content


lines = []
with open(path_to_queries) as f:
    for line in f:
        try:
            content = deal_line(line=line, key_names=["text"])
        except Exception:
            continue

        lines.append(content)

for line in lines:
    full_query = {
        "size": 1,
        "query": {
            "match": {
                field_name: line
            }
        }
    }

    response = client.search(
        body=full_query,
        index=target_index_name
    )
